package com.yiwu.web.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiwu.common.domain.dto.FavoriteDTO;
import com.yiwu.common.domain.entity.UserFavorites;
import com.yiwu.common.domain.mapper.UserFavoritesMapper;
import com.yiwu.common.enums.ResponseCode;
import com.yiwu.common.exception.BusinessException;
import com.yiwu.common.model.vo.favorite.FavoritePageReqVO;
import com.yiwu.common.model.vo.favorite.FavoritePageRspVO;
import com.yiwu.common.model.vo.product.ProductReqVO;
import com.yiwu.web.service.UserFavoritesService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author 咏鹅
 * @description 针对表【UserFavorites】的数据库操作Service实现
 * @createDate 2024-05-25 20:09:18
 */
@Service
public class UserFavoritesServiceImpl implements UserFavoritesService {

    @Resource
    private UserFavoritesMapper userfavoritesMapper;

    @Override
    public Boolean addFavorite(ProductReqVO productReqVO, Long userId) {

        UserFavorites userfavorites = new UserFavorites(userId, productReqVO.getProductId());
        userfavorites.isValid();
        try {
            userfavorites.insert();
        } catch (Exception e) {
            throw new BusinessException(ResponseCode.ADD_FAVORITE_ERROR);
        }
        return true;
    }

    @Override
    public Boolean cancelFavorite(ProductReqVO productReqVO, Long userId) {
        UserFavorites userfavorites = new UserFavorites(userId, productReqVO.getProductId());
        userfavorites.isValid();
        try {
            userfavorites.delete();

        } catch (Exception e) {
            throw new BusinessException(ResponseCode.DEL_FAVORITE_ERROR);
        }
        return true;
    }

    @Override
    public List<FavoritePageRspVO> getFavoriteList(FavoritePageReqVO favoritePageReqVO, Long userId) {
        if (favoritePageReqVO == null || userId == null)
            throw new BusinessException(ResponseCode.SELLER_ERROR);
        Page<FavoriteDTO> page = new Page<>(favoritePageReqVO.getCurrent(), favoritePageReqVO.getSize());
        Page<FavoriteDTO> userFavoriteList = userfavoritesMapper.getUserFavoriteList(page, userId);
        List<FavoriteDTO> userFavoriteRecords = userFavoriteList.getRecords();

        List<FavoritePageRspVO> favoritePageRspVOList = null;
        if (userFavoriteRecords != null) {
            favoritePageRspVOList = userFavoriteRecords.stream()
                    .map(dto -> FavoritePageRspVO.builder()
                            .productId(dto.getProductId().toString())
                            .price(dto.getPrice())
                            .avatarUrl(dto.getAvatarUrl())
                            .pstatus(dto.getPstatus())
                            .description(dto.getDescription())
                            .userName(dto.getUserName())
                            .createdAt(dto.getCreatedAt())
                            .imageUrl(dto.getImageUrl() == null ? "" : dto.getImageUrl().split(";")[0])
                            .build())
                    .toList();
        }
        return favoritePageRspVOList;
    }
}




